package org.sgx.gwteditors.testgallery.gallery;

import java.util.Map;

import org.sgx.gwteditors.client.editor.event.EditorValueChangeEvent;
import org.sgx.gwteditors.client.editor.event.ValueChangeListener;
import org.sgx.gwteditors.client.editor.props.PropertyHaver;
import org.sgx.gwteditors.client.editor.props.PropertyHaverUtil;
import org.sgx.gwteditors.client.editor.props.PropertyInfo;
import org.sgx.gwteditors.client.impl1.EditorFramework1;
import org.sgx.gwteditors.client.impl1.basetypes.Color;
import org.sgx.gwteditors.client.impl1.basetypes.SelectionList;
import org.sgx.gwteditors.client.impl1.complex.Array1DEditor;
import org.sgx.gwteditors.client.impl1.complex.PropertyHaverEditor1;
import org.sgx.gwteditors.client.impl1.edbean.EditableBean;
import org.sgx.gwteditors.client.impl1.simple.ColorEditor;
import org.sgx.gwteditors.client.util.LWOMap;
import org.sgx.gwteditors.client.util.TestUtil;
import org.sgx.gwteditors.testgallery.gallery.BeanEditorTest1.Person;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.resources.client.ExternalTextResource;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;
/**
 * in this test we will show you how to edit your java beans
 * without the propertyHaver methods (these will be added by the framework using gwt derreferred binding. 
 * @author sg
 *
 */
public class BeanEditorTest1 implements Test {

/*---begintestmaincode---*/
@Override
public void test(Panel parent) {
	new EditorFramework1().start(); //do not forget of initialize the gwteditors framework !
	
	VerticalPanel vp = new VerticalPanel();
	vp.setWidth("100%");
	parent.add(vp);	//make sure the editor parent (vp) is attached to the dom before using the editor
	
	PropertyHaverEditor1<Person> ed = new PropertyHaverEditor1<Person>();
	vp.add(ed.getWidget());
	
	Person p1 = GWT.create(Person.class);
	p1.setName("seba"); 
	p1.setAge(28);
	p1.setMale(true);
	p1.setEyeColor(new Color(255,0,0));
	p1.setCategory(new SelectionList<String>(
			new String[]{"defense", "attack", "creation", "energy"}, 
			new String[]{"defense"}));
	ed.load(p1);
//	ed.addValueChangeListener(new ValueChangeListener<Person>() {			
//		@Override
//		public void notifyValueChange(EditorValueChangeEvent<Person> evt) {
//			Window.alert("property value change detected: ");//+evt.getSrc().flush());
//		}
//	});		
}

//the bean we want to edit: 


public static abstract class Person implements EditableBean {
	String name;
	Integer age;
	Boolean male;
	Color eyeColor, nullColor;
	SelectionList<String> category;		
	public Person(){}
	public String getName() {
		return name;
	}		
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public Boolean getMale() {
		return male;
	}
	public void setMale(Boolean male) {
		this.male = male;
	}
	public Color getEyeColor() {
		return eyeColor;
	}
	public void setEyeColor(Color eyeColor) {
		this.eyeColor = eyeColor;
	}
	public SelectionList<String> getCategory() {
		return category;
	}
	public void setCategory(SelectionList<String> category) {
		this.category = category;
	}
	public Color getNullColor() {
		return nullColor;
	}
	public void setNullColor(Color nullColor) {
		this.nullColor = nullColor;
	}
}

/*---endtestmaincode---*/
	
	
	//test stuff
	@Override
	public String getName() {
		return "bean 1";
	}
	@Override
	public Map<String, ExternalTextResource> getJavaSources() {
		return TestUtil.getResource(getName(), TestResources.INSTANCE.BeanEditorTest1());
	}
//	@Override
//	public String[] getJavaSources() {
//		return new String[]{TestResources.INSTANCE.BeanEditorTest1().getText()};
//	}
	@Override
	public String[] getTags() {
		return new String[]{};
	}
	@Override
	public String getDescription() {
		return "show how to use BeanEditor interface so you don't have to implement PropertyHaver getProperties methods. ";
	}
}
